SetGfxDriver 1:Graphics 1024,768,8,2:SeedRnd MilliSecs() :SetBuffer FrontBuffer() 
Global cros,crossafe,crosset,crosout,sort1,out$,in$,insafe$,insafe2$,outlen
out="1"
outlen=54* Len(out)-5
cros=CreateBank(2710):crosset=CreateBank(2710):crosout=CreateBank(2710):crossafe=CreateBank(2710):sort1 = CreateBank(60)
Dim decode(10000):Dim encode(10000):Dim aaa(60)
Codet()

While Not KeyHit(1) 
ClsColor 0,0,0
Cls
setcros()
;                       write bits
Color 210,255,210:Text 280,36,"writed: ",False ,False
For i=1 To Len(out)
	bit = Mid(out$,i,1)
	Color 210,255,210:Text 310+i*6,36,bit,False ,False
	bit=bit+1
	writebit(bit)
	in$=in$+"  ":
Next 	
For u=0 To 378*5 Step 5
	PokeInt crosout,u,PeekInt (cros,u)
Next	
;                       read bits

Color 210,255,210:Text 280,2,"readed: ",False ,False
crosos=False
;While (Not crosos)
	readbit():	insafe$=insafe$+"  ":	insafe2$=insafe2$+"  "
;Wend 	
Color 210,255,210:Text 316,2,in$,False ,False
Color 210,255,210:Text 316,10,insafe$,False ,False
Color 210,255,210:Text 316,18,insafe2$,False ,False
showcros()		
Flip
zzz1=zzz1+1:If zzz1> 0 Then WaitMouse() 
VWait 
Wend 


Function readbit()
	For u=0 To 1885 Step 135

	For unittest=0 To 5	

		eee1=fact(fact(PeekInt(cros,u+0),PeekInt(cros,u+5),PeekInt(cros,u+10)),fact(PeekInt(cros,u+0+15),PeekInt(cros,u+5+15),PeekInt(cros,u+10+15)),fact(PeekInt(cros,u+0+2*15),PeekInt(cros,u+5+2*15),PeekInt(cros,u+10+2*15)))
		eee2=fact(fact(PeekInt(cros,u+9*5+0),PeekInt(cros,u+9*5+5),PeekInt(cros,u+9*5+10)),fact(PeekInt(cros,u+9*5+0+15),PeekInt(cros,u+9*5+5+15),PeekInt(cros,u+9*5+10+15)),fact(PeekInt(cros,u+9*5+0+2*15),PeekInt(cros,u+9*5+5+2*15),PeekInt(cros,u+9*5+10+2*15)))
		eee3=fact(fact(PeekInt(cros,u+18*5+0),PeekInt(cros,u+18*5+5),PeekInt(cros,u+18*5+10)),fact(PeekInt(cros,u+18*5+0+15),PeekInt(cros,u+18*5+5+15),PeekInt(cros,u+18*5+10+15)),fact(PeekInt(cros,u+18*5+0+2*15),PeekInt(cros,u+18*5+5+2*15),PeekInt(cros,u+18*5+10+2*15)))
		eee=fact(eee1,eee2,eee3)
	
		If (eee = unittest) Then untest=unittest^911*10:;insafe$=""+insafe$+""+unittest
;		DebugLog u+" "+in+" "+unittest	+" "+bit		
		
	Next
	
	For unittest=0 To 5	
		For i = 0 To 9 
		PokeInt crosset,u+9*5+i*5,mod2(PeekInt (cros,u+9*5+i*5) - unittest^911*10)
		PokeInt crosset,u+i*5,mod2(PeekInt (cros,u+i*5) )		
		PokeInt crosset,u+18*5+i*5,mod2(PeekInt (cros,u+18*5+i*5) - unittest^911*10 )				
		Next 
		eee1=fact(fact(PeekInt(crosset,u+0),PeekInt(crosset,u+5),PeekInt(crosset,u+10)),fact(PeekInt(crosset,u+0+15),PeekInt(crosset,u+5+15),PeekInt(crosset,u+10+15)),fact(PeekInt(crosset,u+0+2*15),PeekInt(crosset,u+5+2*15),PeekInt(crosset,u+10+2*15)))
		eee2=fact(fact(PeekInt(crosset,u+9*5+0),PeekInt(crosset,u+9*5+5),PeekInt(crosset,u+9*5+10)),fact(PeekInt(crosset,u+9*5+0+15),PeekInt(crosset,u+9*5+5+15),PeekInt(crosset,u+9*5+10+15)),fact(PeekInt(crosset,u+9*5+0+2*15),PeekInt(crosset,u+9*5+5+2*15),PeekInt(crosset,u+9*5+10+2*15)))
		eee3=fact(fact(PeekInt(crosset,u+18*5+0),PeekInt(crosset,u+18*5+5),PeekInt(crosset,u+18*5+10)),fact(PeekInt(crosset,u+18*5+0+15),PeekInt(crosset,u+18*5+5+15),PeekInt(crosset,u+18*5+10+15)),fact(PeekInt(crosset,u+18*5+0+2*15),PeekInt(crosset,u+18*5+5+2*15),PeekInt(crosset,u+18*5+10+2*15)))
		eee=fact(eee1,eee2,eee3)
	
		If (eee = unittest) Then untest=unittest^911*10:insafe$=""+insafe$+""+unittest
;		DebugLog u+" "+in+" "+unittest	+" "+bit		
		
	Next
		For i = 0 To 9 
		PokeInt cros,u+9*5+i*5,mod2(PeekInt (cros,u+9*5+i*5) - untest)
		PokeInt cros,u+i*5,mod2(PeekInt (cros,u+i*5) )		
		PokeInt cros,u+18*5+i*5,mod2(PeekInt (cros,u+18*5+i*5) - untest)	
		Next 
			
	Next	
End Function


Function writebit(bit)
;	bit=bit-1
;	If bit >0 Then
	For u=0 To 1885 Step 135

	For unittest=0 To 5	

		eee1=fact(fact(PeekInt(crosset,u+0),PeekInt(crosset,u+5),PeekInt(crosset,u+10)),fact(PeekInt(crosset,u+0+15),PeekInt(crosset,u+5+15),PeekInt(crosset,u+10+15)),fact(PeekInt(crosset,u+0+2*15),PeekInt(crosset,u+5+2*15),PeekInt(crosset,u+10+2*15)))
		eee2=fact(fact(PeekInt(crosset,u+9*5+0),PeekInt(crosset,u+9*5+5),PeekInt(crosset,u+9*5+10)),fact(PeekInt(crosset,u+9*5+0+15),PeekInt(crosset,u+9*5+5+15),PeekInt(crosset,u+9*5+10+15)),fact(PeekInt(crosset,u+9*5+0+2*15),PeekInt(crosset,u+9*5+5+2*15),PeekInt(crosset,u+9*5+10+2*15)))
		eee3=fact(fact(PeekInt(crosset,u+18*5+0),PeekInt(crosset,u+18*5+5),PeekInt(crosset,u+18*5+10)),fact(PeekInt(crosset,u+18*5+0+15),PeekInt(crosset,u+18*5+5+15),PeekInt(crosset,u+18*5+10+15)),fact(PeekInt(crosset,u+18*5+0+2*15),PeekInt(crosset,u+18*5+5+2*15),PeekInt(crosset,u+18*5+10+2*15)))
		eee=fact(eee1,eee2,eee3)
	
		If (eee = unittest) Then untest=unittest^911*10:;in$=""+in$+""+unittest
;		DebugLog u+" "+in+" "+unittest	+" "+bit		
		
	Next
	
	For unittest=0 To 5	
		For i = 0 To 9 
		PokeInt crosset,u+9*5+i*5,mod2(PeekInt (cros,u+9*5+i*5) - unittest^911*10)
		PokeInt crosset,u+i*5,mod2(PeekInt (cros,u+i*5) )		
		PokeInt crosset,u+18*5+i*5,mod2(PeekInt (cros,u+18*5+i*5) - unittest^911*10 )				
		Next 
		eee1=fact(fact(PeekInt(crosset,u+0),PeekInt(crosset,u+5),PeekInt(crosset,u+10)),fact(PeekInt(crosset,u+0+15),PeekInt(crosset,u+5+15),PeekInt(crosset,u+10+15)),fact(PeekInt(crosset,u+0+2*15),PeekInt(crosset,u+5+2*15),PeekInt(crosset,u+10+2*15)))
		eee2=fact(fact(PeekInt(crosset,u+9*5+0),PeekInt(crosset,u+9*5+5),PeekInt(crosset,u+9*5+10)),fact(PeekInt(crosset,u+9*5+0+15),PeekInt(crosset,u+9*5+5+15),PeekInt(crosset,u+9*5+10+15)),fact(PeekInt(crosset,u+9*5+0+2*15),PeekInt(crosset,u+9*5+5+2*15),PeekInt(crosset,u+9*5+10+2*15)))
		eee3=fact(fact(PeekInt(crosset,u+18*5+0),PeekInt(crosset,u+18*5+5),PeekInt(crosset,u+18*5+10)),fact(PeekInt(crosset,u+18*5+0+15),PeekInt(crosset,u+18*5+5+15),PeekInt(crosset,u+18*5+10+15)),fact(PeekInt(crosset,u+18*5+0+2*15),PeekInt(crosset,u+18*5+5+2*15),PeekInt(crosset,u+18*5+10+2*15)))
		eee=fact(eee1,eee2,eee3)
	
		If (eee = unittest) Then untest=unittest^911*10:in$=""+in$+""+unittest
;		DebugLog u+" "+in+" "+unittest	+" "+bit		
		
	Next
		For i = 0 To 9 
		PokeInt cros,u+9*5+i*5,mod2(PeekInt (cros,u+9*5+i*5) - untest)
		PokeInt cros,u+i*5,mod2(PeekInt (cros,u+i*5) )		
		PokeInt cros,u+18*5+i*5,mod2(PeekInt (cros,u+18*5+i*5) - untest)	
		Next 
	Next 	
;	EndIf
;	For u=0 To 1885  Step 5
;		PokeInt cros,u,PeekInt (cros,u) Xor PeekInt (cros,u+5)
;	Next	
;	For u=1880  To 0 Step -5
;		PokeInt cros,u,PeekInt (cros,u) Xor PeekInt (cros,u+5)
;	Next		
		
End Function





Function showcros()
For i=0 To 378*5 Step 5
 Color 255,255,0:Text 15+((i*19.8) Mod 360),50+12*Int(i/50),PeekInt (crossafe,i) , False, False 

If PeekInt (cros,i)<>PeekInt (crossafe,i)  Then Color 255,0,0 Else  Color 255,255,255
Text 15+((i*19.8) Mod 360),56+12*Int(i/50),PeekInt (cros,i) , False, False 

 Color 255,255,0:Text 515+((i*19.8) Mod 360),50+12*Int(i/50),PeekInt (crosout,i) , False, False 
Next 
End Function

Function setcros()
For i=0 To 378*5 Step 5
t=Rand(0,65535):PokeInt cros,i,t:PokeInt crossafe,i,t:PokeInt crosset,i,t
Next 
End Function

Function mod2(x)
While x>65535
 x = x -65536
Wend 
While  x<0 
 x = x +65536
Wend 
Return x
End Function

Function fact(x1,x2,x3)
        aaa(0)=x1+1:aaa(5)=x2+1:aaa(10)=x3+1
	For x=3 To 1 Step -1
	maxx =0
	For xt=0 To 2
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =0:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5))
	xx = encode(ttt*5)
	Return xx
End Function


Function Codet()
y=0
For x1=1 To 3
For x2=1 To 3
For x3=1 To 3
If((x1 <> x2) And (x1<>x3) And (x2<>x3)) decode(y*5) =x1+""+x2+""+x3:encode(Int(x1+""+x2+""+x3)*5)=y:y=y+1
Next
Next
Next
End Function